package com.hk.newsservice.controller;

import com.github.pagehelper.PageInfo;
import com.hk.commons.CommonResult;
import com.hk.model.NewsType;
import com.hk.model.vo.Result;
import com.hk.model.vo.ResultVO;
import com.hk.newsservice.service.NewsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: aerfazhe
 * @Date: 2021/7/6 9:30
 * @Statement: 新闻类型
 */
@RestController
@RequestMapping("/newsType")
public class NewsTypeController {

    @Autowired
    private NewsTypeService newsTypeService;

    @GetMapping("/findAll")
    public ResultVO<NewsType> findAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "0")Integer limit,
            NewsType newsType
    ) {
        List<NewsType> newsTypeList = newsTypeService.findAll(page, limit, newsType);
        PageInfo<NewsType> newsTypePageInfo = new PageInfo<>(newsTypeList);
        int count = newsTypeService.count(-1,-1, newsType);
        if (newsTypeList.isEmpty()) {
            ResultVO resultVO = CommonResult.nullResultVO(count,newsTypePageInfo.getList());
            return resultVO;
        }
        ResultVO resultVO = CommonResult.successResultVO(count,newsTypePageInfo.getList());
        return resultVO;
//        return null;
    }

    @PostMapping("/save")
    public Result save(@RequestBody(required = false) NewsType newsType) {
        if (newsType == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        newsTypeService.save(newsType);
        Result result = CommonResult.successResult(null);
        return result;
//        return null;
    }

    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable int id) {
        NewsType newsType = newsTypeService.findById(id);
        if (newsType.getId() == 0) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        Result result = CommonResult.successResult(newsType);
        return result;
//        return null;
    }

    @PostMapping("/updateMessage")
    public Result updateMessage(@RequestBody(required = false) NewsType newsType) throws IOException {
        if (newsType == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        newsTypeService.update(newsType);
        Result result = CommonResult.successResult(null);
        return result;
    }
// todo 核查排序(已完毕)
    @PostMapping("/updateParentId")
    public Result updateParentId(@RequestBody(required = false) NewsType newsType) {
        if (newsType.getParentId() == null) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
//        判断修改是否成功，此处具有修改节点是否存在原因，所以必须判断
        int i = newsTypeService.updateParentId(newsType);
        if (i == 0) {
            Result result = CommonResult.nullResult(null);
            return result;
        }
        Result result = CommonResult.successResult(null);
        return result;
//        return null;
    }

    /**
     * 通过Id删除节点及所属子节点
     * @param id
     * @return
     */
    @PostMapping("/deletetNewsType/{id}")
    public Result deletetNewsType(@PathVariable int id) {
        /**
         * 通过Id获取所属子节点
         */
        List<Integer> ids = new ArrayList<>();
        ids.add(id);
        List<NewsType> newsTypeList = newsTypeService.findByParentId(ids);
        //        如果查询无结果就创建一个空集合
        if (newsTypeList.isEmpty()) {
            newsTypeList = new ArrayList<>();
        }
        ids = newsTypeList.stream().map(NewsType::getId).collect(Collectors.toList());
        ids.add(id);
        newsTypeService.deleteIds(ids);
        Result result = CommonResult.successResult(null);
        return result;
    }




}
